#!/bin/bash

# ============================================
# 订单验证脚本
# 用法: ./verify-order.sh <订单编号>
# ============================================

if [ -z "$1" ]; then
    echo "用法: ./verify-order.sh <订单编号>"
    echo "示例: ./verify-order.sh ORD20250113123456789abcdef"
    exit 1
fi

ORDER_NO=$1

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo "=========================================="
echo "订单验证报告: $ORDER_NO"
echo "=========================================="
echo ""

# 1. 查询订单信息
echo -e "${YELLOW}1. 订单基本信息${NC}"
echo "----------------------------------------"
docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 order_db -e "
SELECT 
    order_no as '订单编号',
    user_id as '用户ID',
    total_amount as '订单金额',
    status as '订单状态',
    shipping_address as '收货地址',
    created_at as '创建时间',
    updated_at as '更新时间'
FROM t_order 
WHERE order_no = '$ORDER_NO';
" 2>/dev/null
echo ""

# 2. 查询订单明细
echo -e "${YELLOW}2. 订单明细${NC}"
echo "----------------------------------------"
docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 order_db -e "
SELECT 
    oi.sku_id as 'SKU_ID',
    oi.sku_name as '商品名称',
    oi.price as '单价',
    oi.quantity as '数量',
    oi.subtotal as '小计'
FROM t_order o
JOIN t_order_item oi ON o.id = oi.order_id
WHERE o.order_no = '$ORDER_NO';
" 2>/dev/null
echo ""

# 3. 查询库存操作日志
echo -e "${YELLOW}3. 库存操作记录${NC}"
echo "----------------------------------------"
docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 inventory_db -e "
SELECT 
    sku_id as 'SKU_ID',
    operation_type as '操作类型',
    quantity as '变更数量',
    before_quantity as '操作前',
    after_quantity as '操作后',
    created_at as '操作时间'
FROM t_inventory_log
WHERE order_no = '$ORDER_NO'
ORDER BY created_at;
" 2>/dev/null
echo ""

# 4. 查询支付记录
echo -e "${YELLOW}4. 支付记录${NC}"
echo "----------------------------------------"
docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 payment_db -e "
SELECT 
    payment_no as '支付流水号',
    amount as '支付金额',
    payment_method as '支付方式',
    status as '支付状态',
    payment_time as '支付时间'
FROM t_payment
WHERE order_no = '$ORDER_NO';
" 2>/dev/null
echo ""

# 5. 查询当前库存状态
echo -e "${YELLOW}5. 相关商品库存状态${NC}"
echo "----------------------------------------"
docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 inventory_db -e "
SELECT 
    ps.id as 'SKU_ID',
    ps.sku_code as 'SKU编码',
    ps.sku_name as '商品名称',
    ps.stock_quantity as '总库存',
    ps.reserved_quantity as '预留数量',
    (ps.stock_quantity - ps.reserved_quantity) as '可用库存'
FROM t_product_sku ps
WHERE ps.id IN (
    SELECT DISTINCT oi.sku_id 
    FROM t_order o
    JOIN t_order_item oi ON o.id = oi.order_id
    WHERE o.order_no = '$ORDER_NO'
);
" 2>/dev/null
echo ""

# 6. 验证订单状态
echo -e "${YELLOW}6. 订单状态验证${NC}"
echo "----------------------------------------"
ORDER_STATUS=$(docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 order_db -se "SELECT status FROM t_order WHERE order_no = '$ORDER_NO';" 2>/dev/null | tr -d '\r\n')

if [ "$ORDER_STATUS" = "PAID" ]; then
    echo -e "${GREEN}✓ 订单状态正常: $ORDER_STATUS${NC}"
    echo -e "${GREEN}✓ 订单已完成支付流程${NC}"
elif [ "$ORDER_STATUS" = "PENDING" ]; then
    echo -e "${YELLOW}⚠ 订单状态: $ORDER_STATUS${NC}"
    echo -e "${YELLOW}⚠ 订单可能还在处理中，请稍后再查询${NC}"
else
    echo -e "${RED}✗ 订单状态异常: $ORDER_STATUS${NC}"
fi
echo ""

# 7. 事件流转验证
echo -e "${YELLOW}7. 事件流转验证${NC}"
echo "----------------------------------------"

# 检查库存日志
INVENTORY_LOG_COUNT=$(docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 inventory_db -se "SELECT COUNT(*) FROM t_inventory_log WHERE order_no = '$ORDER_NO';" 2>/dev/null | tr -d '\r\n')

if [ "$INVENTORY_LOG_COUNT" -gt 0 ]; then
    echo -e "${GREEN}✓ 库存预留事件已处理 (记录数: $INVENTORY_LOG_COUNT)${NC}"
else
    echo -e "${RED}✗ 未找到库存操作记录${NC}"
fi

# 检查支付记录
PAYMENT_COUNT=$(docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 payment_db -se "SELECT COUNT(*) FROM t_payment WHERE order_no = '$ORDER_NO';" 2>/dev/null | tr -d '\r\n')

if [ "$PAYMENT_COUNT" -gt 0 ]; then
    echo -e "${GREEN}✓ 支付事件已处理 (记录数: $PAYMENT_COUNT)${NC}"
else
    echo -e "${RED}✗ 未找到支付记录${NC}"
fi
echo ""

# 8. 总结
echo "=========================================="
if [ "$ORDER_STATUS" = "PAID" ] && [ "$INVENTORY_LOG_COUNT" -gt 0 ] && [ "$PAYMENT_COUNT" -gt 0 ]; then
    echo -e "${GREEN}✓✓✓ 订单流程验证通过 ✓✓✓${NC}"
    echo ""
    echo "完整流程已执行:"
    echo "  1. ✓ 订单创建成功"
    echo "  2. ✓ 库存预留成功"
    echo "  3. ✓ 支付处理成功"
    echo "  4. ✓ 订单状态已更新"
else
    echo -e "${YELLOW}⚠ 订单流程可能未完成${NC}"
    echo ""
    echo "请检查:"
    echo "  - 所有服务是否正常运行"
    echo "  - Kafka 是否正常工作"
    echo "  - 查看服务日志是否有错误"
fi
echo "=========================================="
echo ""

